/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.samples.explorer.client;

import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.widget.ContentPanel;
import net.mygwt.ui.client.widget.WidgetContainer;
import net.mygwt.ui.client.widget.layout.FillLayout;
import net.mygwt.ui.client.widget.table.EditTableItem;
import net.mygwt.ui.client.widget.table.NumberCellRenderer;
import net.mygwt.ui.client.widget.table.TableColumn;
import net.mygwt.ui.client.widget.table.TableColumnModel;
import net.mygwt.ux.client.widgets.table.EditTable;
import net.mygwt.ux.samples.explorer.client.data.Folder;
import net.mygwt.ux.samples.explorer.client.data.Stock;
import net.mygwt.ux.samples.explorer.client.data.TestData;

import com.google.gwt.i18n.client.NumberFormat;

/**
 * 
 * @author simon
 *
 */
public class EditTablePage extends Page {

  protected void createWidget(WidgetContainer container) {
    final NumberFormat currency = NumberFormat.getCurrencyFormat();
    final NumberFormat number = NumberFormat.getFormat("0.00");

    TableColumn[] columns = new TableColumn[5];

    columns[0] = new TableColumn("Company", 0.20f);
    columns[0].setMinWidth(75);
    columns[0].setMaxWidth(300);

    columns[1] = new TableColumn("Symbol", 0.20f);

    columns[2] = new TableColumn("Last", 0.20f);
    columns[2].setMaxWidth(100);
    columns[2].setAlignment(Style.RIGHT);
    columns[2].setRenderer(new NumberCellRenderer(currency));

    columns[3] = new TableColumn("Change", 0.20f);
    columns[3].setAlignment(Style.RIGHT);
//    columns[3].renderer = new CellRenderer() {
//      public String render(String property, Object value) {
//        double val = ((Double) value).doubleValue();
//        String style = val < 0 ? "red" : "green";
//        return "<span style='color:" + style + "'>" + number.format(val) + "</span>";
//      }
//    };

    columns[4] = new TableColumn("Last Updated", 0.20f);
    columns[4].setAlignment(Style.RIGHT);
//    columns[4].renderer = new DateTimeCellRenderer("MM/d/y");

    TableColumnModel cm = new TableColumnModel(columns);

    EditTable tbl = new EditTable(Style.MULTI | Style.HORIZONTAL, cm);
    tbl.setBorders(false);

    Folder stocks = TestData.getStocks();
    for (int i = 0; i < stocks.getChildCount(); i++) {
      Stock stock = (Stock) stocks.getChild(i);
      Object[] values = new Object[5];
      values[0] = stock.getName();
      values[1] = stock.getSymbol();
      values[2] = new Double(stock.getLast());
      values[3] = new Double(stock.getLast() - stock.getOpen());
      values[4] = stock.getLastTrans();

      EditTableItem item = new EditTableItem(values);
      tbl.add(item);
    }

    ContentPanel panel = new ContentPanel(Style.HEADER | Style.COLLAPSE);
    panel.setAnimateCollapse(false);
    panel.setText("Table Demo");
    panel.setLayout(new FillLayout());
    panel.add(tbl);

    container.setLayout(new FillLayout(15));
    container.add(panel);
  }

}
